<script setup>
import { ref, onMounted, watch, computed } from 'vue'
import { ElMessage, ElLoading } from 'element-plus'
// 导入rank.js中的接口方法
import { getRankList } from '@/api/rank'
import { getAllSongList } from '@/api/song-list'
import { getAllConsumer } from '@/api/consumer'


const playlistRatings = ref([]) // 存储评分列表数据
const queryParams = ref({
  pageNo: 1,    // 当前页码
  pageSize: 6,  // 每页数量
  keyword: ''   // 搜索关键词（歌单名称）
})
const total = ref(0) // 总记录数
const loading = ref(false) // 加载状态
const songListMap = ref({}) // 歌单列表id映射表
const consumerMap = ref({}) // 用户信息映射表

// 获取评分列表数据
const fetchRankData = async () => {
  try {
    loading.value = true;
    let data;
    // 获取所有歌单列表
    ({ data } = await getAllSongList());
    data.forEach(songList => {
      songListMap.value[songList.id] = songList.title
    });
    // 获取所有用户信息
    ({ data } = await getAllConsumer());
    data.forEach(consumer => {
      consumerMap.value[consumer.id] = consumer.username
    });
    // 获取评分列表数据
    ({ data } = await getRankList(queryParams.value));
    playlistRatings.value = data.records;
    total.value = data.total
    // 遍历评分列表，添加歌单名称、用户姓名
    playlistRatings.value.forEach(item => {
      // 根据歌单ID查找对应的歌单名称
      item.songListName = songListMap.value[item.songListId] || `未知歌单(id: ${item.songListId})`
      // 根据用户ID查找对应的用户名
      item.consumerId = consumerMap.value[item.consumerId] || `未知用户(id: ${item.consumerId})`
    })
    
  } catch (error) {
    console.error('获取评分数据异常:', error)
    ElMessage.error('网络异常，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 分页切换处理
const handlePageChange = (page) => {
  queryParams.value.pageNo = page
  fetchRankData()
}

// 组件挂载时加载数据
onMounted(() => {
  fetchRankData()
})

const search = ref('') // 搜索关键词

</script>

<template>
  <el-card style="max-width: 100%">
    <template #header>
      <div class="card-header">
        <h2>歌单评分展示</h2>
      </div>
    </template>

    <!-- 评分列表表格 -->
    <el-table v-loading="loading" :data="playlistRatings" stripe style="width: 100%">
      <!-- 歌单名称列 -->
      <el-table-column prop="songListName" label="歌单名称" min-width="180" />
      
      <!-- 评分列（星级+数值） -->
      <el-table-column label="评分" width="400">
        <template #default="{ row }">
          <div class="rating-box">
            <!-- 星级展示 -->
            <div class="star-rating">
              <span 
                class="star" 
                v-for="i in 10" 
                :key="i" 
                :class="{ active: i <= row.score }"
              >★</span>
              <span 
                class="star half" 
                v-if="row.score % 1 !== 0"
              >★</span>
            </div>
            <!-- 数值展示 -->
            <span class="score-value">{{ row.score }}分</span>
          </div>
        </template>
      </el-table-column>

      <!-- 评价用户列 -->
      <el-table-column prop="consumerId" label="评价用户" min-width="280" />
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container" v-if="total > 0">
      <el-pagination
        background
        layout="prev, pager, next, jumper"
        :total="total"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="handlePageChange"
      />
    </div>
  </el-card>
</template>

<style scoped>
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.rating-box {
  display: flex;
  align-items: center;
  gap: 8px;
}

.star-rating {
  display: flex;
  position: relative;
  font-size: 20px;
}

.star {
  color: #eee;
  transition: color 0.2s;
}

.star.active {
  color: #ffc107; /* 激活的星星颜色 */
}

.half {
  position: absolute;
  left: -10px;
  width: 10px;
  overflow: hidden;
  color: #ffc107;
}

.score-value {
  font-weight: bold;
  color: #333;
  min-width: 50px;
}

.user-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.user-avatar-text {
  display: flex;
  align-items: center;
  gap: 4px;
}

.username {
  font-size: 14px;
  color: #606266;
  white-space: nowrap;
}

.more-tip {
  font-size: 14px;
  color: #909399;
  margin-left: auto;
}

.pagination-container {
  margin-top: 20px;
  text-align: center;
}

/* 添加加载状态的样式 */
:deep(.el-table__body-wrapper) {
  min-height: 300px;
}
</style>